package com.onclick.webchat.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/view")
public class ViewController {
	private static final StringBuffer uuids=new StringBuffer(); 
	private static final Map<String,Object> map = new HashMap<>();
    @RequestMapping(value="/index",method=RequestMethod.GET)
	public String getIndex(HttpSession httpSession,Model model) {
    	model.addAttribute("message", "首页");
  
		return "/index";
		
	}
    @RequestMapping(value="/webchat",method=RequestMethod.GET)
     public String gotoWebChat(HttpSession httpSession,Model model) {
    	boolean flag = true;
    	if (map.size() !=0) {
    		for (Entry<String, Object> iterable_element : map.entrySet()) {
    			if (iterable_element.getValue().equals(httpSession.getId())) {
    				flag = false;
    				break;
    			}
    		}
		}
    
    	
    	System.out.println(" 请求跳转");
    	if (flag) {
			
		
    UUID uuid = UUID.randomUUID();
    	httpSession.setAttribute("privateId", uuid.toString());
    	model.addAttribute("privateIds", uuid.toString());
    	uuids.append(uuid.toString());
    	map.put(uuid.toString(), httpSession.getId());
      	}else {
      	  	httpSession.setAttribute("privateId", uuids.toString());
        	model.addAttribute("privateIds", uuids.toString());
      	}
		return "/webchat";
  
     }
    
}
